﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ATA.Business;
using ATA.Entities;
using Infragistics.Shared;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinSchedule;

using System.Configuration;

namespace ATA.Winforms.Admin
{
    public partial class SearchHoiVien : Form
    {
        #region "Variables"
        TList<KhachHang> KhachHangList;
        ValueList _DaiLyValueList=new ValueList();
        ValueList _GioiTinhValueList = new ValueList();
        ValueList _LoaiHoiVienValueList = new ValueList();
        int previousMemberTypeValue = -2;
        #endregion

        #region "Control Events"
        public SearchHoiVien()
        {
            try
            {
                InitializeComponent();  
                ultraGrid2.DisplayLayout.Override.RowSelectors = DefaultableBoolean.True;
                ultraGrid2.DisplayLayout.Override.RowSelectorNumberStyle = RowSelectorNumberStyle.VisibleIndex;
                loadValueListsForGridCell();
                RefreshForm();          
            }
            catch (Exception ex)
            {
                
            }            
        }

        //private void btnRefresh_Click_1(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        RefreshForm();
        //    }
        //    catch (Exception ex)
        //    {

                
        //    }
        //}

        private void ultraGrid2_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            try
            {
                foreach (UltraGridBand b in ultraGrid2.DisplayLayout.Bands)
                {
                    foreach (UltraGridColumn c in b.Columns)
                    {
                        if ((c.Key.ToString().Equals("Id")) || (c.Key.ToString().Equals("KhachHangId")) || (c.Key.ToString().Equals("MaKh")) ||
                            (c.Key.ToString().Equals("LoaiKh")) || (c.Key.ToString().Equals("SoHoaDon")) ||
                            (c.Key.ToString().Equals("NgayLayHang")) || (c.Key.ToString().Equals("DoanhSo")) || (c.Key.ToString().Equals("GhiChu")) ||
                            (c.Key.ToString().Equals("NgayDangKy")))
                        {
                            c.Hidden = true;
                            c.ExcludeFromColumnChooser = ExcludeFromColumnChooser.True;
                        }

                        if (c.Key.ToString().Equals("DaiLyId"))
                            c.Header.Caption = "Đại Lý";
                        if (c.Key.ToString().Equals("TenKh"))
                            c.Header.Caption = "Tên Khách Hàng";
                        if (c.Key.ToString().Equals("GioiTinh"))
                            c.Header.Caption = "Giới Tính";
                        if (c.Key.ToString().Equals("NgaySinh"))
                        {
                            c.Header.Caption = "Ngày Sinh";
                            c.Format = "dd/MM/yyyy";
                            c.Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Date;
                        }                            
                        if (c.Key.ToString().Equals("Cmnd"))
                            c.Header.Caption = "CMND";
                        if (c.Key.ToString().Equals("NgayCap"))
                        { 
                            c.Header.Caption = "Ngày Cấp";
                            c.Format = "dd/MM/yyyy";
                            c.Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Date;
                        }
                        if (c.Key.ToString().Equals("NoiCap"))
                            c.Header.Caption = "Nơi Cấp";
                        if (c.Key.ToString().Equals("SoDtDk"))
                            c.Header.Caption = "Số Điện Thoại";
                        if (c.Key.ToString().Equals("SoDtCty"))
                            c.Header.Caption = "Số Điện Thoại Công Ty";
                        if (c.Key.ToString().Equals("Fax"))
                            c.Header.Caption = "Fax";
                        if (c.Key.ToString().Equals("SoGpdkkd"))
                            c.Header.Caption = "Số GPDKKD";
                        if (c.Key.ToString().Equals("DiaChiTt"))
                            c.Header.Caption = "Địa Chỉ";
                        if (c.Key.ToString().Equals("HoKhauTt"))
                            c.Header.Caption = "Hộ Khẩu";
                        if (c.Key.ToString().Equals("LoaiHoiVien"))
                            c.Header.Caption = "Loại Hội Viên";
                        if (c.Key.ToString().Equals("MaSoHv"))
                            c.Header.Caption = "Mã Số Hội Viên";
                        if (c.Key.ToString().Equals("DiaChiCty"))
                            c.Header.Caption = "Địa Chỉ Công Ty";
                        if (c.Key.ToString().ToLower().Equals("parentid"))
                            c.Hidden = true;
                        if (c.Key.ToString().ToLower().Equals("branchid"))
                            c.Hidden = true;
                    }
                }
            }
            catch (Exception ex)
            {
                
            }    
        }

        //private void btnSearch_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        KhachHangService ks = new KhachHangService();
        //        int Total = 0;
        //        string WHEREstring = "1=1 ";

        //        if (txtCMT.Value != null)
        //            WHEREstring = string.Format("{0} and cmnd like '%{1}%'", WHEREstring, txtCMT.Value.ToString());
        //        if (!string.IsNullOrEmpty(txtDiaChiCongTy.Text))
        //            WHEREstring = string.Format("{0} and Dia_Chi_Cty like '%{1}%'", WHEREstring, txtDiaChiCongTy.Text);
        //        if (!string.IsNullOrEmpty(txtDiaChiTamTru.Text))
        //            WHEREstring = string.Format("{0} and Dia_Chi_TT like '%{1}%'", WHEREstring, txtDiaChiTamTru.Text);
        //        if (!string.IsNullOrEmpty(txtFax.Text))
        //            WHEREstring = string.Format("{0} and Fax like '%{1}%'", WHEREstring, txtFax.Text);
        //        if (!string.IsNullOrEmpty(txtHoKhauThuongTru.Text))
        //            WHEREstring = string.Format("{0} and Ho_Khau_TT like '%{1}%'", WHEREstring, txtHoKhauThuongTru.Text);
        //        if (!string.IsNullOrEmpty(txtMaSoHoiVien.Text))
        //            WHEREstring = string.Format("{0} and Ma_So_HV like '%{1}%'", WHEREstring, txtMaSoHoiVien.Text);
        //        if (!string.IsNullOrEmpty(txtNoiCap.Text))
        //            WHEREstring = string.Format("{0} and Noi_Cap like '%{1}%'", WHEREstring, txtNoiCap.Text);
        //        if (!string.IsNullOrEmpty(txtSoDienThoai.Text))
        //            WHEREstring = string.Format("{0} and So_DT_DK like '%{1}%'", WHEREstring, txtSoDienThoai.Text);
        //        if (!string.IsNullOrEmpty(txtSoDienThoaiCongTy.Text))
        //            WHEREstring = string.Format("{0} and So_DT_CTY like '%{1}%'", WHEREstring, txtSoDienThoaiCongTy.Text);
        //        if (!string.IsNullOrEmpty(txtSoGPDKKD.Text))
        //            WHEREstring = string.Format("{0} and So_GPDKKD like '%{1}%'", WHEREstring, txtSoGPDKKD.Text);
        //        if (!string.IsNullOrEmpty(txtTenKhachHang.Text))
        //            WHEREstring = string.Format("{0} and Ten_KH like '%{1}%'", WHEREstring, txtTenKhachHang.Text);
        //        if (drpGioiTinh.SelectedItem != null)
        //        {
        //            if (drpGioiTinh.SelectedItem.DataValue != null)
        //                WHEREstring = string.Format("{0} and DIFFERENCE(Gioi_Tinh,'{1}') = 4", WHEREstring, drpGioiTinh.SelectedItem.DataValue.ToString());
        //        }
        //        if (drpDaiLy.SelectedItem != null)
        //        {
        //            if (drpDaiLy.SelectedItem.DataValue != null)
        //                WHEREstring = string.Format("{0} and Dai_Ly_ID = {1}", WHEREstring, drpDaiLy.SelectedItem.DataValue.ToString());
        //        }
        //        if (drpNgayCapCMT.Value.ToString() != string.Empty)
        //            WHEREstring = string.Format("{0} and convert(varchar(10),Ngay_Cap,101) = convert(varchar(10),convert(datetime,'{1}'),101)", WHEREstring, drpNgayCapCMT.Value.ToString());
        //        if (drpNgaySinh.Value.ToString() != string.Empty)
        //            WHEREstring = string.Format("{0} and convert(varchar(10),Ngay_Sinh,101) = convert(varchar(10),convert(datetime,'{1}'),101)", WHEREstring, drpNgaySinh.Value.ToString());
        //        if (rdoLoaiTheHoiVien.CheckedItem != null)
        //            WHEREstring = string.Format("{0} and DIFFERENCE(Loai_Hoi_Vien,'{1}') = 4", WHEREstring, rdoLoaiTheHoiVien.CheckedItem.DataValue.ToString());

        //        KhachHangList = ks.GetPaged(WHEREstring, "Ten_KH ASC", 0, int.MaxValue, out Total);
        //        if (KhachHangList != null)
        //        {
        //            if (KhachHangList.Count > 0)
        //            {
        //                ultraGrid2.DataSource = KhachHangList;
        //                lblMessage.Text = KhachHangList.Count.ToString() + " Bản Ghi Được Tìm Thấy!";
        //            }
        //            else
        //            {
        //                lblMessage.Text = "Chương Trình Không Tìm Thấy Bản Ghi!";
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        lblMessage.Text="Chương Trình Không Thể Tìm Kiếm!";
        //        //
        //    }
        //}

        //private void btnUpdate_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        KhachHang SearchedKhachHang;
        //        string error = string.Empty;
        //        lblMessage.Text = string.Empty;

        //        foreach (UltraGridRow _Row in ultraGrid2.Rows)
        //        {
        //            SearchedKhachHang = KhachHangList.Find("KhachHangId", (int)_Row.Cells["KhachHangId"].Value);
        //            if (SearchedKhachHang != null)
        //            {                        
        //                error = IsError(_Row);
        //                if (!string.IsNullOrEmpty(error))
        //                {
        //                    lblMessage.Text = string.Format("{0}{1}",lblMessage.Text, error);
        //                }
        //                else
        //                {
        //                    if (IsChanged(_Row) == true)
        //                    {
        //                        SearchedKhachHang.TenKh = (string)_Row.Cells["TenKh"].Value;
        //                        SearchedKhachHang.Cmnd = (string)_Row.Cells["Cmnd"].Value;
        //                        SearchedKhachHang.DaiLyId = (int)_Row.Cells["DaiLyId"].Value;//(int)_Row.Cells["DaiLyId"].ValueListResolved.GetValue(_Row.Cells["DaiLyId"].ValueListResolved.SelectedItemIndex);
        //                        SearchedKhachHang.DiaChiCty = (string)_Row.Cells["DiaChiCty"].Value;
        //                        SearchedKhachHang.DiaChiTt = (string)_Row.Cells["DiaChiTt"].Value;
        //                        SearchedKhachHang.Fax = (string)_Row.Cells["Fax"].Value;
        //                        SearchedKhachHang.GioiTinh = (string)_Row.Cells["GioiTinh"].Value;
        //                        SearchedKhachHang.HoKhauTt = (string)_Row.Cells["HoKhauTt"].Value;
        //                        SearchedKhachHang.LoaiHoiVien = (string)_Row.Cells["LoaiHoiVien"].Value;
        //                        SearchedKhachHang.MaSoHv = (string)_Row.Cells["MaSoHv"].Value;
        //                        SearchedKhachHang.NgayCap = (DateTime)_Row.Cells["NgayCap"].Value;
        //                        SearchedKhachHang.NgaySinh = (DateTime)_Row.Cells["NgaySinh"].Value;
        //                        SearchedKhachHang.NoiCap = (string)_Row.Cells["NoiCap"].Value;
        //                        SearchedKhachHang.SoDtCty = (string)_Row.Cells["SoDtCty"].Value;
        //                        SearchedKhachHang.SoDtDk = (string)_Row.Cells["SoDtDk"].Value;
        //                        SearchedKhachHang.SoGpdkkd = (string)_Row.Cells["SoGpdkkd"].Value;

        //                        (new KhachHangService()).Update(SearchedKhachHang);
        //                        lblMessage.Text = string.Format("{0}Cập nhật thành công bản ghi số {1}{2}", lblMessage.Text, (_Row.Index + 1).ToString(), Environment.NewLine);
        //                    }
        //                }
        //            }
        //        }
        //        MessageBox.Show(lblMessage.Text);
        //    }
        //    catch (Exception ex)
        //    {
        //        lblMessage.Text = "Chương Trình Không Thể Cập Nhật!";
                
        //    }
        //}

        private void ultraGrid2_InitializeRow(object sender, InitializeRowEventArgs e)
        {
            try
            {
                e.Row.Cells["DaiLyId"].ValueList = _DaiLyValueList;
                e.Row.Cells["GioiTinh"].ValueList = _GioiTinhValueList;
                e.Row.Cells["LoaiHoiVien"].ValueList = _LoaiHoiVienValueList;
            }
            catch (Exception ex)
            {
                
                
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            try
            {
                KhachHangService ks = new KhachHangService();
                int Total = 0;
                string WHEREstring = "1=1 ";

                if (txtCMT.Value != null)
                    WHEREstring = string.Format("{0} and cmnd like '%{1}%'", WHEREstring, txtCMT.Value.ToString());
                if (!string.IsNullOrEmpty(txtDiaChiCongTy.Text))
                    WHEREstring = string.Format("{0} and Dia_Chi_Cty like '%{1}%'", WHEREstring, txtDiaChiCongTy.Text);
                if (!string.IsNullOrEmpty(txtDiaChiTamTru.Text))
                    WHEREstring = string.Format("{0} and Dia_Chi_TT like '%{1}%'", WHEREstring, txtDiaChiTamTru.Text);
                if (!string.IsNullOrEmpty(txtFax.Text))
                    WHEREstring = string.Format("{0} and Fax like '%{1}%'", WHEREstring, txtFax.Text);
                if (!string.IsNullOrEmpty(txtHoKhauThuongTru.Text))
                    WHEREstring = string.Format("{0} and Ho_Khau_TT like '%{1}%'", WHEREstring, txtHoKhauThuongTru.Text);
                if (!string.IsNullOrEmpty(txtMaSoHoiVien.Text))
                    WHEREstring = string.Format("{0} and MaSoHV like '%{1}%'", WHEREstring, txtMaSoHoiVien.Text);
                if (!string.IsNullOrEmpty(txtNoiCap.Text))
                    WHEREstring = string.Format("{0} and Noi_Cap like '%{1}%'", WHEREstring, txtNoiCap.Text);
                if (!string.IsNullOrEmpty(txtSoDienThoai.Text))
                    WHEREstring = string.Format("{0} and So_DT_DK like '%{1}%'", WHEREstring, txtSoDienThoai.Text);
                if (!string.IsNullOrEmpty(txtSoDienThoaiCongTy.Text))
                    WHEREstring = string.Format("{0} and So_DT_CTY like '%{1}%'", WHEREstring, txtSoDienThoaiCongTy.Text);
                if (!string.IsNullOrEmpty(txtSoGPDKKD.Text))
                    WHEREstring = string.Format("{0} and So_GPDKKD like '%{1}%'", WHEREstring, txtSoGPDKKD.Text);
                if (!string.IsNullOrEmpty(txtTenKhachHang.Text))
                    WHEREstring = string.Format("{0} and Ten_KH like '%{1}%'", WHEREstring, txtTenKhachHang.Text);
                if (drpGioiTinh.SelectedItem != null)
                {
                    if (drpGioiTinh.SelectedItem.DataValue != null)
                        WHEREstring = string.Format("{0} and DIFFERENCE(Gioi_Tinh,'{1}') = 4", WHEREstring, drpGioiTinh.SelectedItem.DataValue.ToString());
                }
                if (cboBranch.SelectedItem != null)
                {
                    if (cboBranch.SelectedItem.DataValue != null)
                        WHEREstring = string.Format("{0} and BranchID = '{1}'", WHEREstring, cboBranch.SelectedItem.DataValue.ToString());
                }
                if (drpDaiLy.SelectedRow != null)
                {

                    if (drpDaiLy.SelectedRow.Cells["AccountingObjectID"].Value != null)
                    {
                        WHEREstring = string.Format("{0} and Parent_ID = '{1}'", WHEREstring, drpDaiLy.SelectedRow.Cells["AccountingObjectID"].Value); 
                    }
                }
                if (drpNgayCapCMT.Value.ToString() != string.Empty)
                    WHEREstring = string.Format("{0} and convert(varchar(10),Ngay_Cap,101) = convert(varchar(10),convert(datetime,'{1}'),101)", WHEREstring, drpNgayCapCMT.Value.ToString());
                if (drpNgaySinh.Value.ToString() != string.Empty)
                    WHEREstring = string.Format("{0} and convert(varchar(10),Ngay_Sinh,101) = convert(varchar(10),convert(datetime,'{1}'),101)", WHEREstring, drpNgaySinh.Value.ToString());
                if (rdoLoaiTheHoiVien.CheckedItem != null)
                    WHEREstring = string.Format("{0} and Loai_KH = {1}) = 4", WHEREstring, rdoLoaiTheHoiVien.CheckedItem.DataValue.ToString());

                KhachHangList = ks.GetPaged(WHEREstring, "Ten_KH ASC", 0, int.MaxValue, out Total);
                if (KhachHangList != null)
                {
                    if (KhachHangList.Count > 0)
                    {
                        ultraGrid2.DataSource = KhachHangList;
                        lblMessage.Text = KhachHangList.Count.ToString() + " Bản Ghi Được Tìm Thấy!";
                    }
                    else
                    {
                        lblMessage.Text = "Chương Trình Không Tìm Thấy Bản Ghi!";
                    }
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = "Chương Trình Không Thể Tìm Kiếm!";
                //
            }
        }

        private void btnLamTuoi_Click(object sender, EventArgs e)
        {
            try
            {
                RefreshForm();
            }
            catch (Exception ex)
            {


            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                KhachHang SearchedKhachHang;
                string error = string.Empty;
                lblMessage.Text = string.Empty;

                foreach (UltraGridRow _Row in ultraGrid2.Rows)
                {
                    SearchedKhachHang = KhachHangList.Find("KhachHangId", (int)_Row.Cells["KhachHangId"].Value);
                    if (SearchedKhachHang != null)
                    {
                        error = IsError(_Row);
                        if (!string.IsNullOrEmpty(error))
                        {
                            lblMessage.Text = string.Format("{0}{1}", lblMessage.Text, error);
                        }
                        else
                        {
                            if (IsChanged(_Row) == true)
                            {
                                SearchedKhachHang.TenKh = (string)_Row.Cells["TenKh"].Value;
                                SearchedKhachHang.Cmnd = (string)_Row.Cells["Cmnd"].Value;
                                SearchedKhachHang.ParentId = (Guid)_Row.Cells["DaiLyId"].Value;//(int)_Row.Cells["DaiLyId"].ValueListResolved.GetValue(_Row.Cells["DaiLyId"].ValueListResolved.SelectedItemIndex);
                                SearchedKhachHang.DiaChiCty = (string)_Row.Cells["DiaChiCty"].Value;
                                SearchedKhachHang.DiaChiTt = (string)_Row.Cells["DiaChiTt"].Value;
                                SearchedKhachHang.Fax = (string)_Row.Cells["Fax"].Value;
                                SearchedKhachHang.GioiTinh = (string)_Row.Cells["GioiTinh"].Value;
                                SearchedKhachHang.HoKhauTt = (string)_Row.Cells["HoKhauTt"].Value;
                                SearchedKhachHang.LoaiKh = (short)_Row.Cells["LoaiHoiVien"].Value;
                                SearchedKhachHang.MaKh = (string)_Row.Cells["MaSoHv"].Value;
                                SearchedKhachHang.MaSoHv = (string)_Row.Cells["MaSoHv"].Value;
                                SearchedKhachHang.NgayCap = (DateTime)_Row.Cells["NgayCap"].Value;
                                SearchedKhachHang.NgaySinh = (DateTime)_Row.Cells["NgaySinh"].Value;
                                SearchedKhachHang.NoiCap = (string)_Row.Cells["NoiCap"].Value;
                                SearchedKhachHang.SoDtCty = (string)_Row.Cells["SoDtCty"].Value;
                                SearchedKhachHang.SoDtDk = (string)_Row.Cells["SoDtDk"].Value;
                                SearchedKhachHang.SoGpdkkd = (string)_Row.Cells["SoGpdkkd"].Value;

                                (new KhachHangService()).Update(SearchedKhachHang);
                                lblMessage.Text = string.Format("{0}Cập nhật thành công bản ghi số {1}{2}", lblMessage.Text, (_Row.Index + 1).ToString(), Environment.NewLine);
                            }
                        }
                    }
                }
                MessageBox.Show(lblMessage.Text);
            }
            catch (Exception ex)
            {
                lblMessage.Text = "Chương Trình Không Thể Cập Nhật!";

            }
        }
        #endregion

        #region "Custom Methods"
        private bool IsChanged(UltraGridRow _Row)
        {
            if ((string)_Row.Cells["TenKh"].OriginalValue != (string)_Row.Cells["TenKh"].Value ||
                (string)_Row.Cells["Cmnd"].OriginalValue != (string)_Row.Cells["Cmnd"].Value ||
                (int)_Row.Cells["DaiLyId"].OriginalValue != (int)_Row.Cells["DaiLyId"].Value ||
                (string)_Row.Cells["DiaChiCty"].OriginalValue != (string)_Row.Cells["DiaChiCty"].Value ||
                (string)_Row.Cells["DiaChiTt"].OriginalValue != (string)_Row.Cells["DiaChiTt"].Value ||
                (string)_Row.Cells["Fax"].OriginalValue != (string)_Row.Cells["Fax"].Value ||
                (string)_Row.Cells["GioiTinh"].OriginalValue != (string)_Row.Cells["GioiTinh"].Value ||
                (string)_Row.Cells["HoKhauTt"].OriginalValue != (string)_Row.Cells["HoKhauTt"].Value ||
                (string)_Row.Cells["LoaiHoiVien"].OriginalValue != (string)_Row.Cells["LoaiHoiVien"].Value ||
                (string)_Row.Cells["MaSoHv"].OriginalValue != (string)_Row.Cells["MaSoHv"].Value ||
                (DateTime)_Row.Cells["NgayCap"].OriginalValue != (DateTime)_Row.Cells["NgayCap"].Value ||
                (DateTime)_Row.Cells["NgaySinh"].OriginalValue != (DateTime)_Row.Cells["NgaySinh"].Value ||
                (string)_Row.Cells["NoiCap"].OriginalValue != (string)_Row.Cells["NoiCap"].Value ||
                (string)_Row.Cells["SoDtCty"].OriginalValue != (string)_Row.Cells["SoDtCty"].Value ||
                (string)_Row.Cells["SoDtDk"].OriginalValue != (string)_Row.Cells["SoDtDk"].Value ||
                (string)_Row.Cells["SoGpdkkd"].OriginalValue != (string)_Row.Cells["SoGpdkkd"].Value
                )
                return true;            
            else
                return false;
        }

        private string IsError(UltraGridRow _Row)
        {
            string Error = string.Empty;
            string i = (_Row.Index + 1).ToString();
            if (_Row.Cells["TenKh"].Value.ToString() == string.Empty)
                Error = string.Format("Bản ghi số {0}: Yêu cầu nhập tên khách hàng!{1}", i, System.Environment.NewLine);

            if (_Row.Cells["NgaySinh"].Value == null)
                Error = string.Format("{0}Bản ghi số {1}: Yêu cầu nhập ngày sinh!{2}", Error, i, System.Environment.NewLine);

            if (_Row.Cells["DiaChiTt"].Value.ToString() == string.Empty)
                Error = string.Format("{0}Bản ghi số {1}: Yêu cầu nhập địa chỉ tạm trú!{2}", Error, i, System.Environment.NewLine);

            if (_Row.Cells["HoKhauTt"].Value.ToString() == string.Empty)
                Error = string.Format("{0}Bản ghi số {1}: Yêu cầu nhập nơi đăng ký hộ khẩu thường trú!{2}", Error, i, System.Environment.NewLine);

            if (_Row.Cells["Cmnd"].Value.ToString() == string.Empty)
                Error = string.Format("{0}Bản ghi số {1}: Yêu cầu nhập số chứng minh thư!{2}", Error, i, System.Environment.NewLine);

            if (_Row.Cells["NgayCap"].Value == null)
                Error = string.Format("{0}Bản ghi số {1}: Yêu cầu nhập ngày cấp chứng minh thư!{2}", Error, i, System.Environment.NewLine);

            if (_Row.Cells["NoiCap"].Value.ToString() == string.Empty)
                Error = string.Format("{0}Bản ghi số {1}: Yêu cầu nhập nơi cấp chứng minh thư!{2}", Error, i, System.Environment.NewLine);

            if (_Row.Cells["SoDtDk"].Value.ToString() == string.Empty)
                Error = string.Format("{0}Bản ghi số {1}: Yêu cầu nhập số điện thoại!{2}", Error, i, System.Environment.NewLine);
            return Error;
        }

        private void loadValueListsForGridCell()
        {
            TList<KhachHang> DaiLyList = (new KhachHangService()).Find("ISACTIVE = TRUE");

            if (DaiLyList != null)
            {
                if (DaiLyList.Count > 0)
                {
                    foreach (KhachHang _DaiLy  in DaiLyList)
                    {
                        _DaiLyValueList.ValueListItems.Add(_DaiLy.ParentId, _DaiLy.TenKh);
                    }
                }
            }             
                
            _GioiTinhValueList.ValueListItems.Add("Nam", "Nam");
            _GioiTinhValueList.ValueListItems.Add("Nữ", "Nữ");                
            
            _LoaiHoiVienValueList.ValueListItems.Add("Nhà thầu", "Nhà thầu");
            _LoaiHoiVienValueList.ValueListItems.Add("Giám đốc dự án", "Giám đốc dự án");
            _LoaiHoiVienValueList.ValueListItems.Add("Tổng công ty xây dựng, dự án lớn", "Tổng công ty xây dựng, dự án lớn");
            _LoaiHoiVienValueList.ValueListItems.Add("Chủ nhà mua lẻ", "Chủ nhà mua lẻ");                
        }
        
        private void RefreshForm()
        {
            //loadDrpDaiLy();
            drpGioiTinh.SelectedIndex = 0;
            drpGioiTinh.Items[0].ResetDataValue();
            drpGioiTinh.Items[0].ResetDisplayText();
            drpNgayCapCMT.Text = string.Empty;
            drpNgaySinh.Text = string.Empty;
            //Utility.setEmptyTextToTextBoxes(this);
            //lblMessage.Text = string.Empty;
        }

        private void LoadDrpDaiLy()
        {
            //TList<KhachHang> DaiLyList = (new KhachHangService()).Find("ISACTIVE = TRUE");

            //if (DaiLyList != null)
            //{
            //    if (DaiLyList.Count > 0)
            //    {
            //        drpDaiLy.DataSource = DaiLyList;
            //        drpDaiLy.DisplayMember = "TenDaiLy";
            //        drpDaiLy.ValueMember = "DaiLyID";
            //        drpDaiLy.SelectedIndex = 0;
            //    }
            //}

            drpDaiLy.DisplayMember = "AccountingObjectCode";
            drpDaiLy.ValueMember = "AccountingObjectID";
            drpDaiLy.DataSource = Globals.gv_lsCustomers;
            drpDaiLy.DisplayLayout.Bands[0].Columns["AccountingObjectID"].Hidden = true;
            drpDaiLy.DisplayLayout.Bands[0].Columns["ParentAccountingObjectId"].Hidden = true;
            drpDaiLy.DisplayLayout.Bands[0].Columns["AccountingObjectCode"].Header.Caption = "Mã KH";
            drpDaiLy.DisplayLayout.Bands[0].Columns["AccountingObjectCode"].Width = 80;
            drpDaiLy.DisplayLayout.Bands[0].Columns["AccountingObjectName"].Header.Caption = "Tên KH";
            drpDaiLy.DisplayLayout.Bands[0].Columns["AccountingObjectName"].Width = 250;
            drpDaiLy.AutoCompleteMode = Infragistics.Win.AutoCompleteMode.SuggestAppend;

        }

        private void LoadBranch()
        {
            #region Load chi nhanh
            ATA.Data.MisaDAL.DAL dal = new ATA.Data.MisaDAL.DAL(ConfigurationManager.ConnectionStrings["misaConnectionString"].ConnectionString, ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
            DataSet v_dsBranches;

            if (Globals.gv_bIsGlobalBranch)
            {
                v_dsBranches = dal.GetAllActiveBranchs();
                cboBranch.DataSource = v_dsBranches;
                cboBranch.DataMember = v_dsBranches.Tables[0].TableName;
                cboBranch.Enabled = true;
            }
            else
            {
                v_dsBranches = dal.GetActiveBranchsByID(Globals.gv_strBranchID);
                cboBranch.DataSource = v_dsBranches;
                cboBranch.DataMember = v_dsBranches.Tables[0].TableName;
                cboBranch.SelectedIndex = 0;
                cboBranch.Enabled = false;
            }

            cboBranch.DisplayMember = "BranchName";
            cboBranch.ValueMember = "BranchID";
            cboBranch.DataBind();
            #endregion

        }
        #endregion         

        private void SearchHoiVien_Load(object sender, EventArgs e)
        {
            LoadBranch();
            LoadDrpDaiLy();
        }

        private void rdoLoaiTheHoiVien_Click(object sender, EventArgs e)
        {
            if (rdoLoaiTheHoiVien.CheckedIndex == previousMemberTypeValue)
            {
                rdoLoaiTheHoiVien.CheckedIndex = -1;
            }

        }

        private void rdoLoaiTheHoiVien_ValueChanged(object sender, EventArgs e)
        {

            previousMemberTypeValue = rdoLoaiTheHoiVien.CheckedIndex;
        }

    }
}
